package com.kasa.gps.tool;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Vibrator;
import android.util.Log;
import android.widget.Toast;

import com.kasa.gps.OrderGPSActivity;
import com.kasa.gps.OrderGPSMapActivity;
import com.kasa.gps.R;
import com.kasa.gps.db.OrderGPSDBAdapter;

public class Tool {
	
	public static final String SPLIT = "-";

	//年、月、日分隔符
	public static final String SPLIT_Y_M_D = "-";
	//日期、时间分隔符
	public static final String SPLIT_DATE_TIME = " ";
	//时、分分隔符
	public static final String SPLIT_H_M = ":";
	//服务器地址和端口的分隔符
	public static final String SPLIT_ADDR_PORT = ":";
	
	public static final DateFormat ALARM_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	
	public static final byte ID_0 = 0;
	public static final byte ID_1 = 1;
	public static final byte ID_2 = 2;
	
	private static String[] frequencyArray = null;
	private static String[] timeUnitsArray = null;
	private static String[] distanceUnitsArray = null;
	public final byte UNITS_TIME = 0;
	public final byte UNITS_DISTANCE = 1;
	
	
	public  static final float TEXT_SIZE = 16f;
	
	public static final String CLOSE_SERVICE_KEY = "CLOSE_SERVICE";
	public static final String REMARKS_TEXT_KEY = "REMARKS_TEXT_KEY";
	
	public Tool(Context context){
		if(frequencyArray==null || timeUnitsArray==null || distanceUnitsArray == null){
			frequencyArray = context.getResources().getStringArray(R.array.frequency_list);
			timeUnitsArray = context.getResources().getStringArray(R.array.timeUnits_list);
			distanceUnitsArray = context.getResources().getStringArray(R.array.distanceUnits_list);
			
		}
	}

	
	public static void showMessage(Context context,String msg){
		Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
	}
	
	/**
	 * units value
	 * {@link UNITS_TIME}
	 * ,
	 * {@link UNITS_DISTANCE}
	 * */
	public String convert(String frequency,byte units){
		String []f = frequency.split(Tool.SPLIT);
		return frequencyArray[Byte.parseByte(f[0])]+(units == UNITS_TIME?timeUnitsArray[Byte.parseByte(f[1])]:distanceUnitsArray[Byte.parseByte(f[1])]);
	}
	
	/**
	 * units value
	 * {@link UNITS_TIME}
	 * ,
	 * {@link UNITS_DISTANCE}
	 * */
	public long getFrequency(String frequency,byte units){
		long num = 5000;
		String []f = frequency.split(SPLIT);
		
		try {
			byte numId = Byte.parseByte(f[0]);
			byte unitId = Byte.parseByte(f[1]);
			switch(units){
			case UNITS_TIME:
				if(unitId == ID_0){
					num = Integer.parseInt(frequencyArray[numId]) * 1000;
				}else if(unitId == ID_1){
					num = Integer.parseInt(frequencyArray[numId]) * 60000;
				}else if(unitId == ID_2){
					num = Integer.parseInt(frequencyArray[numId]) * 3600000;
				}
				break;
			case UNITS_DISTANCE:
				if(unitId == ID_0){
					num = Integer.parseInt(frequencyArray[numId]) * 10;
				}else if(unitId == ID_1){
					num = Integer.parseInt(frequencyArray[numId]) * 100;
				}else if(unitId == ID_2){
					num = Integer.parseInt(frequencyArray[numId]) * 1000;
				}
				break;
			}
			
		} catch (NumberFormatException e) {
			num = 5000;
		} catch (IndexOutOfBoundsException e) {
			num = 5000;
		}
		
		
		return num;
	}
	
	/**后台服务是否正在运行*/
	public static boolean isServiceRunning(Context context,String serviceClassName){
		ActivityManager manager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningServiceInfo> serviceList = manager.getRunningServices(10000);
		if(serviceList == null || serviceList.size() <= 0){
			return false;
		}
		for(RunningServiceInfo service:serviceList){
			if(service.service.getClassName().equals(serviceClassName)){
				return true;
			}
		}
		return false;
	}
	
	/**退出应用程序*/
//	public static void exitApp(Context context){
	    //退出应用程序
//        Intent exit = new Intent(Intent.ACTION_MAIN);
//        exit.addCategory(Intent.CATEGORY_HOME);
//        exit.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        context.startActivity(exit);
//        System.exit(0);
//	}
	
	public static void backToMain(Context context){
		Intent main = new Intent(context, OrderGPSActivity.class);
		main.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//清空当前context 的activity 
		context.startActivity(main);
	}
	
	/**网络是否可用*/
	public static boolean isNetworkOK(Context context){
		ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if(connectivityManager ==null){
			return false;
		}
		
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
		if(networkInfo == null){
			return false;
		}
		return networkInfo.isAvailable() && networkInfo.isConnected();
	}
	
	private static Vibrator  vibrator = null;
	/**打开振动*/
	public static void vibrateOn(Context context){
		if(vibrator == null){
			vibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
			vibrator.vibrate(new long[]{1000,50,1000,50,1000}, 0);
			Log.i("vibrateOn", "vibrateOn...");
		}
	}
	/**关闭振动*/
	public static void vibrateOff(){
		if(vibrator!=null){
			vibrator.cancel();
			vibrator = null;
			Log.i("vibrateOff", "vibrateOff...");
		}
	}
	
}
